package com.xyt.imei.core.model.converter;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xyt.imei.core.meta.TraceEventEnum;
import com.xyt.imei.core.model.po.Trace;
import com.xyt.imei.core.model.po.User;
import com.xyt.imei.core.model.vo.TraceVO;
import com.xyt.imei.core.service.UserService;

@Component
public class TraceConverter {

	@Autowired
	private UserService userService;
	
	public List<TraceVO> po2vo(List<Trace> traceList) throws Exception {
		
		List<TraceVO> vos = new ArrayList<TraceVO>();
		
		for(Trace trace : traceList) {
			vos.add(po2vo(trace));
		}
		
		return vos;
	}
	
	public TraceVO po2vo(Trace trace) throws Exception {
		
		TraceVO vo = new TraceVO();
		
		vo.setImeiNo(trace.getImeiNo());
		
		User sourceUser = userService.findByPrimaryKey(trace.getSourceUserId());
		User targetUser = userService.findByPrimaryKey(trace.getTargetUserId());
		
		if(sourceUser != null)
			vo.setSourceUser(sourceUser.getUsername());
		
		if(targetUser != null)
			vo.setTargetUser(targetUser.getUsername());
		
		vo.setEvent(TraceEventEnum.valueOf(trace.getEvent()));
		vo.setCreateDate(trace.getCreateDate());
		
		return vo;
		
	}
	
}
